﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Lambda表達式學習 : System.Web.UI.Page
{

    string testStr = "";


    /// <summary>
    /// 運行一個沒有返回值的方法
    /// </summary>
    delegate void RunAMethod();

   /// <summary>
   /// 運行帶兩個參數的方法
   /// </summary>
   /// <returns>字符串</returns>
    delegate string RuARetrunValueMethod(int intObj,String strObj);

    /// <summary>
    ///  將Lambdm表達式分給委託
    /// </summary>
    /// <returns></returns>
    delegate double 將Lambdm表達式分給委託(double i);

    protected void Page_Load(object sender, EventArgs e)
    {


        #region 使用 一般 變量 (蠻量,變量2)=> 


        //(int sb, string 東東) =>
        //{
        //    sb = 1; 東東 = "dfsdfds";

        //    testStr = 東東;
           
        //};

        //運行帶兩個參數的方法

        var 東東 = new RuARetrunValueMethod(
             (i, s) =>
             {
                 Response.Write("<br/>運行帶兩個參數的方法");

                 return "我是返回的:" + s;
             });

        Response.Write(東東.DynamicInvoke(1, "2"));
        


        #endregion

        #region 使用()方法 ()=>一些方法裏的代碼

        ///運行一個沒有返回值的方法
       var sc= new RunAMethod(
            () => { Response.Write("運行一個沒有返回值的方法<br/>"); });

        //運行這個方法
       sc.Invoke();




        #endregion

        #region 將表達式分配給委託類型
        //示例代碼
        //delegate int del(int i);
        //del myDelegate = x => x * x;
        //int j = myDelegate(5); //j = 25创建表达式目录树类型： 


       將Lambdm表達式分給委託 testDeleagte = temp => temp * temp + 25; 

        Response.Write(
       testDeleagte.DynamicInvoke(2)
        );
        #endregion
    }


    delegate bool D();
    delegate bool D2(int i);

    class Test
    {
        D del;
        D2 del2;
        public void TestMethod(int input)
        {
            int j = 0;
            // Initialize the delegates with lambda expressions.
            // Note access to 2 outer variables.
            // del will be invoked within this method.
            del = () => { j = 10; return j > input; };

            // del2 will be invoked after TestMethod goes out of scope.
            del2 = (x) => { return x == j; };

            // Demonstrate value of j:
            // Output: j = 0 
            // The delegate has not been invoked yet.
            Console.WriteLine("j = {0}", j);

            // Invoke the delegate.
            bool boolResult = del();

            // Output: j = 10 b = True
            Console.WriteLine("j = {0}. b = {1}", j, boolResult);
        }

         void Test方法()
        {
            Test test = new Test();
            test.TestMethod(5);

            // Prove that del2 still has a copy of
            // local variable j from TestMethod.
            bool result = test.del2(10);

            // Output: True
            Console.WriteLine(result);

            Console.ReadKey();
        }
    }
}
